package com.dkd.sbtz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.sbtz.domain.AssetFile;
import com.dkd.sbtz.service.IAssetFileService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理Controller
 * 
 * @author xzj
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/sbtz/file")
public class AssetFileController extends BaseController
{
    @Autowired
    private IAssetFileService assetFileService;

    /**
     * 查询文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetFile assetFile)
    {
        startPage();
        List<AssetFile> list = assetFileService.selectAssetFileList(assetFile);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetFile assetFile)
    {
        List<AssetFile> list = assetFileService.selectAssetFileList(assetFile);
        ExcelUtil<AssetFile> util = new ExcelUtil<AssetFile>(AssetFile.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 获取文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assetFileService.selectAssetFileById(id));
    }

    /**
     * 新增文件管理
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetFile assetFile)
    {
        return toAjax(assetFileService.insertAssetFile(assetFile));
    }

    /**
     * 修改文件管理
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:edit')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetFile assetFile)
    {
        return toAjax(assetFileService.updateAssetFile(assetFile));
    }

    /**
     * 删除文件管理
     */
    @PreAuthorize("@ss.hasPermi('sbtz:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(assetFileService.deleteAssetFileByIds(ids));
    }




    /** 上传：multipart form-data => files[] + relativePaths[] */
    @PostMapping(value = "/apiUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult apiUpload(
            @RequestPart("files") List<MultipartFile> files,
            @RequestParam(value = "relativePaths", required = false) List<String> relativePaths
    ) {
        assetFileService.uploadBatch(files, relativePaths);
        return AjaxResult.success();
    }



    @DeleteMapping("/apidel/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        assetFileService.softDelete(id);
        return AjaxResult.success();
    }


    @PostMapping(value = "/replace/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult replace(@PathVariable Long id,
                              @RequestPart("file") MultipartFile file,
                              @RequestParam(value = "relativePath", required = false) String relativePath) {
        assetFileService.replaceFile(id, file, relativePath);
        return AjaxResult.success();
    }


    @PostMapping(value = "/apiUpWithField", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult apiUpload(
            @RequestPart("files") List<MultipartFile> files,
            @RequestParam(value = "relativePaths", required = false) List<String> relativePaths,
            @RequestParam(value = "bizTag", required = false) String bizTag,
            @RequestParam(value = "remark", required = false) String remark
    ) {
        if (files == null || files.isEmpty()) {
            return AjaxResult.error("请选择文件");
        }
        List<AssetFile> saved = assetFileService.saveFiles(files, relativePaths, bizTag, remark);
        Map<String, Object> data = new HashMap<>();
        data.put("rows", saved);
        data.put("count", saved.size());
        return AjaxResult.success(data);
    }

}
